#include "userinfowindow.h"
#include "inputwindow.h"
#include <QVBoxLayout>
#include <QLabel>
#include <QFile>
#include <QJsonDocument>
#include <QJsonObject>

UserInfoWindow::UserInfoWindow(QWidget *parent) : QMainWindow(parent)
{
    setWindowTitle("🌟 BMI评估系统 - 用户登录");
    resize(600, 500);
    setStyleSheet("background-color: #f8f9fa;");

    setupUI();
}

void UserInfoWindow::setupUI()
{
    QLabel *titleLabel = new QLabel("📝 请输入用户信息", this);
    titleLabel->setStyleSheet(
        "font-size: 20pt; "
        "font-weight: bold; "
        "color: #6c757d; "
        "font-family: 'Microsoft YaHei';"
    );

    QLabel *userLabel = new QLabel("👤 昵称:", this);
    userLabel->setStyleSheet("font-size: 14pt; color: #495057;");
    usernameInput = new QLineEdit(this);
    usernameInput->setPlaceholderText("请输入您的昵称");
    usernameInput->setStyleSheet(
        "QLineEdit {"
        "  font-size: 14pt;"
        "  padding: 10px;"
        "  border: 2px solid #dee2e6;"
        "  border-radius: 15px;"
        "  background-color: white;"
        "}"
    );

    QLabel *pwdLabel = new QLabel("🔒 BMI管理密码:", this);
    pwdLabel->setStyleSheet("font-size: 14pt; color: #495057;");
    passwordInput = new QLineEdit(this);
    passwordInput->setPlaceholderText("请设置您的密码");
    passwordInput->setEchoMode(QLineEdit::Password);
    passwordInput->setStyleSheet(
        "QLineEdit {"
        "  font-size: 14pt;"
        "  padding: 10px;"
        "  border: 2px solid #dee2e6;"
        "  border-radius: 15px;"
        "  background-color: white;"
        "}"
    );

    submitBtn = new QPushButton("✨ 开始BMI评估", this);
    submitBtn->setStyleSheet(
        "QPushButton {"
        "  font-size: 14pt;"
        "  padding: 12px 24px;"
        "  background-color: #4cc9f0;"
        "  color: white;"
        "  border-radius: 15px;"
        "  border: none;"
        "}"
        "QPushButton:hover {"
        "  background-color: #4895ef;"
        "}"
    );

    connect(submitBtn, &QPushButton::clicked, [this]() {
        QString username = usernameInput->text().trimmed();
        QString password = passwordInput->text().trimmed();

        if(username.isEmpty() || password.isEmpty()) {
            QMessageBox::warning(this, "提示", "请输入昵称和密码");
            return;
        }

        saveUserInfo(username, password);
        emit userAuthenticated(username);
    });

    QVBoxLayout *layout = new QVBoxLayout();
    layout->addWidget(titleLabel);
    layout->addWidget(userLabel);
    layout->addWidget(usernameInput);
    layout->addWidget(pwdLabel);
    layout->addWidget(passwordInput);
    layout->addWidget(submitBtn);
    layout->addStretch();

    QWidget *centralWidget = new QWidget(this);
    centralWidget->setLayout(layout);
    setCentralWidget(centralWidget);
}

void UserInfoWindow::saveUserInfo(const QString &username, const QString &password)
{
    QFile file("userdata.json");
    QJsonObject userData;
    if(file.open(QIODevice::ReadWrite)) {
        QJsonDocument doc = QJsonDocument::fromJson(file.readAll());
        if(!doc.isNull()) {
            userData = doc.object();
        }
        
        QJsonObject user;
        user["password"] = password;
        userData[username] = user;
        
        file.resize(0);
        file.write(QJsonDocument(userData).toJson());
        file.close();
    }
}